<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/26
 * Time: 13:59
 */

namespace app\common;


use app\common\model\Sellers;
use app\common\model\Trade;
use think\Db;

class OrderStatus
{
    const DAIZHIFU = 1100;     #待支付
    const YIZHIFU = 1200;      #已支付 待发货 待接单
    const DAIFAHUO = 1200;     #待发货  待接单
    const YIJIEDAI = 1300;     #已接单
//    const DAIYUNSHU = 1400;    #待运输
//    const DAIZITI = 1500;      #待自提
//    const YITIHUO = 1600;      #已提货
//    const DAISONGDA = 1700;    #待送达
//    const DAISHOUHUO = 1800;   #待收货
    const QUERENSHOUHUO = 1900;#确认收货  确认收货  已配送
//    const DAIPINGJIA = 2000;   #用户消费者确认收货待评价
//    const YIPINGJIA = 2100;    #已评价
//    const TUIHUO = 2200;       #退货
    const BEGIN_AFTER_SALE = 2205;       #售后
//    const END_AFTER_SALE = 2206;       #结束售后
//    const FINISH = 2300;       #已完成

    /**
     * 商家/管理员 订单状态
     */
    static function get_order_status()
    {
        $order_status = [
            self::DAIZHIFU => '待支付',  //1100
            self::DAIFAHUO => '待接单',  //1200
            self::YIJIEDAI => '已接单',  //1300
            self::QUERENSHOUHUO => '已配送',  //1200
            self::BEGIN_AFTER_SALE => '售后',  //1200
//            self::DAIYUNSHU => '待运输',  //1400 #送到自提点或者用户地址（自提点状态省略）
//            self::DAIZITI . ',' . self::YITIHUO . ',' . self::DAISONGDA . ',' . self::DAISHOUHUO . ',' . self::QUERENSHOUHUO => '待送达',  //1500-1900
//            self::DAIPINGJIA => '已收货',  //2000
//            self::YIPINGJIA => '已评价',   //2100
        ];
        /*
        $order_status = [
            self::DAIZHIFU =>'待支付',
            self::DAIFAHUO =>'待发货',
            self::DAIYUNSHU => '待运输',
            self::DAIZITI => '待自提',
            self::YITIHUO => '已提货',
            self::DAISONGDA => '待送达',
            self::DAISHOUHUO =>'自提点待收货',
            self::QUERENSHOUHUO =>'自提点确认收货',
            self::DAIPINGJIA => '待评价 ',
            self::DAISONGDA => '已评价 ',
            self::TUIHUO => '退货',
            self::FINISH => '已完成',
        ]; */
        return $order_status;
    }


    private static function change_status($order_id, $order_status_id)
    {

        $status = Db::table("shop_order")->where(["order_id" => $order_id])->update([
            "order_status_id" => $order_status_id
        ]);
        return $status;
    }

    public static function daizhifu($order_id)
    {

        $status = self::change_status($order_id, self::DAIZHIFU);
        return $status;
    }

    public static function yijiedan(\app\common\model\Order $order)
    {
        $order->order_status_id = self::YIJIEDAI;
        $status = $order->save();
        return $status;
    }

    public static function daiyunshu($order_id)
    {

        $status = self::change_status($order_id, self::DAIYUNSHU);
        return $status;
    }

    public static function yizhifu(\app\common\model\Order $order)
    {
        $order->order_status_id = self::YIZHIFU;
        $order->pay_time = date("Y-m-d H:i:s");
        $status = $order->save();
        Notify::seller_send($order->seller_id, ["router" => "seller/order/list", "msg" => "您有新的订单", "code" => 1001]);
        return $status;
    }

    public static function querenshouhuo(\app\common\model\Order $order)
    {
        $order->order_status_id = self::QUERENSHOUHUO;
        $status = $order->save();
        return $status;
    }

    /**
     * 开启售后
     * @param model\Order $order
     * @return false|int
     */
    public static function begin_after_sale(\app\common\model\Order $order)
    {
        $status = $order->where(["order_id" => $order->order_id])->update(["order_status_id" => self::BEGIN_AFTER_SALE]);
        return $status;
    }


    public static function rebates(\app\common\model\Order $order, $memo = '')
    {
        if ($order->rebates == 0) {
            /** @var Sellers $seller */
            $seller = Sellers::get(["id" => $order->seller_id]);
            /** @var \app\common\model\Users $user */
            $user = $seller->get_user();
            if (!empty($user)) {
                Trade::create_rebates($user, $order, $memo);
                $order->rebates = 1;
                $order->save();
            }
        }
        return false;
    }

}